package com.ruoyi.mdkt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.mdkt.entity.Absent;
import com.ruoyi.mdkt.entity.Course;
import com.ruoyi.mdkt.entity.CourseInfo;
import com.ruoyi.mdkt.entity.vo.AbsentVo;
import com.ruoyi.mdkt.entity.vo.CourseVo;
import com.ruoyi.mdkt.entity.vo.ShowAbsentVo;
import com.ruoyi.mdkt.mapper.AbsentMapper;
import com.ruoyi.mdkt.service.AbsentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.mdkt.service.CourseService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gwl
 * @since 2022-10-25
 */
@Service
public class AbsentServiceImpl extends ServiceImpl<AbsentMapper, Absent> implements AbsentService {

    @Autowired
    private AbsentMapper absentMapper;
    @Autowired
    private AbsentService absentService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private CourseService courseService;

    @Override
    public List<AbsentVo> getAbsentList(Long courseId) {

        return absentMapper.getAbsentList(String.valueOf(courseId));

    }

    @Override
    public AbsentVo getAbsentInfo(Long absentID) {
        return absentMapper.getAbsentInfo(String.valueOf(absentID));
    }

    @Override
    public Page<ShowAbsentVo> pageList(Absent absent, int pageNum, int pageSize) {
        LambdaQueryWrapper<Absent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(absent.getStudentName()!= null,Absent::getStudentName,absent.getStudentName());
        Page<Absent> absentPage = new Page<>(pageNum,pageSize);
        Page<ShowAbsentVo> showAbsentVoPage = new Page<>(pageNum,pageSize);
        absentService.page(absentPage,wrapper);
        BeanUtils.copyProperties(absentPage,showAbsentVoPage,"records");
        List<Absent> records = absentPage.getRecords();
        List<ShowAbsentVo> collect = records.stream().map((item) -> {
            ShowAbsentVo showAbsentVo = new ShowAbsentVo();
            BeanUtils.copyProperties(item, showAbsentVo);
            showAbsentVo.setTeacherName(getTeacherName(item.getTeacherUserId()));
            showAbsentVo.setCourseName(getCourseName(item.getCourseId()));
            return showAbsentVo;
        }).collect(Collectors.toList());
        showAbsentVoPage.setRecords(collect);
        return showAbsentVoPage;
    }
    private String getCourseName(Long courseId) {
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Course::getId,courseId);
        Course one = courseService.getOne(wrapper);
        if (one == null){
            return null;
        }
        return one.getCourseName();
    }
    private String getTeacherName(Long teacherUserId) {
        SysUser sysUser = iSysUserService.selectUserById(teacherUserId);

        return sysUser.getNickName();
    }
}
